package cloud.xiangyixiang.www.spring.boot.java.basics.utils;

import cloud.xiangyixiang.www.spring.boot.java.basics.pojo.bo.UserBo;

import java.util.Arrays;

/**
 * <p>
 * 数组Clone工具类
 * </p>
 *
 * @author weixin
 * @date 2023/8/31 20:00
 */
public class ArrayCloneUtil {

    /**
     * 基础数组类型克隆-整型数组
     */
    public static void IntArrayClone(){
        int[] array = new int[]{1, 0, 0, 8, 6};
        int[] array2;

        array2 = array.clone();

        System.out.println("改变前：=================");
        System.out.println("array: " + Arrays.toString(array));
        System.out.println("array2: " + Arrays.toString(array2));

        System.out.println("改变后：=================");
        array2[0] = 100;
        System.out.println("array: " + Arrays.toString(array));
        System.out.println("array2: " + Arrays.toString(array2));

        System.out.println();
    }

    /**
     * 对象数组克隆
     */
    public static void ObjectArrayCloner() {

        System.out.println("对象数组克隆：=================");

        UserBo user1 = new UserBo();
        user1.setUsername("ZhangYiBo");
        user1.setAge(18);

        UserBo user2 = new UserBo();
        user2.setUsername("ZhangJing");
        user2.setAge(17);

        UserBo[] users1 = new UserBo[2];
        users1[0] = user1;
        users1[1] = user2;

        UserBo[] users2 = users1.clone();

        System.out.println("改变前：");
        System.out.println("users1：" + Arrays.toString(users1));
        System.out.println("users2：" + Arrays.toString(users2));


        System.out.println("改变后：");
        users1[0].setAge(30);
        System.out.println("users1：" + Arrays.toString(users1));
        System.out.println("users2：" + Arrays.toString(users2));

        System.out.println();
    }

}
